﻿using Dapper;
using ProTrack.Entity;
using ProTrack.IRepository;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Linq;
using ProTrack.Dto;
using Org.BouncyCastle.Crypto;
using ProTrack.IRepository.WareHouse;
using ProTrack.IServices.Stock;

namespace ProTrack.Services.Stock
{
    /// <summary>
    /// 生产领料业务逻辑层
    /// </summary>
    public class OrderProductPickServices : IOrderProductPickServices
    {
        public OrderProductPickServices(IBaseRepository db, IOrderProductPickRepository or)
        {
            OR = or;
            DB = db;
        }

        private IOrderProductPickRepository OR { get; }
        private IBaseRepository DB { get; }
        public async Task<bool> AddAsync(OrderProductPick model, List<OrderProductPickItem> list)
        {
            var result = await OR.AddAsync(model, list);
            return result;
        }

        public async Task<bool> DeleteAsync(int[] Ids)
        {
            var result = await OR.DeleteAsync(Ids);
            return result;
        }

        public async Task<bool> DeleteAsync(int Id)
        {
            var result = await OR.DeleteAsync(Id);
            return result;
        }

        public async Task<IEnumerable<OrderProductPick>> GetAllAsync()
        {
            string sql = "select * from OrderProductPick";
            var list = await DB.QueryAllAsync<OrderProductPick>(sql);
            return list;
        }

        public async Task<IEnumerable<OrderProductPick>> GetAsync(int PageIndex, int PageSize, string Number, string PickDate, string WarehouseName, string EmployeesName, int? Status)
        {
            string sql = "select * from OrderProductPick where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number";
            }
            if (!string.IsNullOrEmpty(PickDate))
            {
                sql += " and PickDate like @PickDate";
            }
            if (!string.IsNullOrEmpty(WarehouseName))
            {
                sql += " and WarehouseName like @WarehouseName";
            }
            if (!string.IsNullOrEmpty(EmployeesName))
            {
                sql += " and EmployeesName like @EmployeesName";
            }
            if (!string.IsNullOrEmpty(Status.ToString()))
            {
                sql += " and Status like @Status";
            }
            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only";
            var list = await DB.QueryAsync<OrderProductPick>(sql, new { PageIndex, PageSize, Number = $"%{Number}%", PickDate = $"%{PickDate}%", WarehouseName = $"%{WarehouseName}%", EmployeesName = $"%{EmployeesName}%", Status = $"%{Status}%" });
            return list;
        }

        public async Task<IEnumerable<OrderProductPickItemDto>> GetBomItembyId(string WareHouseId, string ItemId)
        {
            var list = await DB.QueryAsync<OrderProductPickItemDto>("select pb.BomItemId as ItemId,pb.BomItemCode as ItemCode,pb.BomItemName as ItemName,i.Model,pb.BomItemSpec as Spec,i.Kind,pb.UnitOfMeasure,(ik.StockCount - ik.SalesCount) as CurrCount,ik.WareHouseId,pb.Quantity as Count from ProductBom pb inner join ItemStock ik on ik.WareHouseId=@WareHouseId and ik.ItemId=pb.BomItemId inner join Item i on i.Id = pb.BomItemId where pb.ItemId = @ItemId", new { WareHouseId, ItemId });
            return list;
        }

        public async Task<int> GetCountAsync(string Number, string PickDate, string WarehouseName, string EmployeesName, int? Status)
        {
            string sql = "select count(*) from OrderProductPick where 1=1";
            if (!string.IsNullOrEmpty(Number))
            {
                sql += " and Number like @Number";
            }
            if (!string.IsNullOrEmpty(PickDate))
            {
                sql += " and PickDate like @PickDate";
            }
            if (!string.IsNullOrEmpty(WarehouseName))
            {
                sql += " and WarehouseName like @WarehouseName";
            }
            if (!string.IsNullOrEmpty(EmployeesName))
            {
                sql += " and EmployeesName like @EmployeesName";
            }
            if (!string.IsNullOrEmpty(Status.ToString()))
            {
                sql += " and Status like @Status";
            }
            var count = await DB.GetRecoreCountAsync(sql, new { Number = $"%{Number}%", PickDate = $"%{PickDate}%", WarehouseName = $"%{WarehouseName}%", EmployeesName = $"%{EmployeesName}%", Status = $"%{Status}%" });
            return count;
        }

        public async Task<IEnumerable<OrderProductPickItemDto>> GetOrderProductPickbyIdAsync(string OrderProductPickId,string WareHouseId)
        {
            var list = await DB.QueryAsync<OrderProductPickItemDto>("SELECT op.ItemId,i.ItemCode,i.ItemName,i.Model,i.Spec,i.Kind,i.UnitOfMeasure,(ik.StockCount - ik.SalesCount) AS CurrCount,op.Count,ik.WareHouseId FROM Item i INNER JOIN ItemStock ik ON i.Id = ik.ItemId AND ik.WareHouseId = @WareHouseId INNER JOIN OrderProductPickItem op ON i.Id = op.ItemId WHERE op.OrderProductPickId = @OrderProductPickId", new { OrderProductPickId, WareHouseId });
            return list;
        }

        public async Task<bool> UpdateAsync(OrderProductPick model, List<OrderProductPickItem> list)
        {
            var result = await OR.UpdateAsync(model, list);
            return result;
        }

        public async Task<bool> UpdateStatus(long Id,int Status)
        {
            string sql = $"update OrderProductPick set Status=@Status where Id=@Id";
            var result = await DB.UpdateAsync(sql, new { Id, Status });
            return result;
        }

    }
}
